'''
$LastChangedBy: csgillespie $
$LastChangedDate: 2007-06-06 11:08:06 +0000 (Wed, 06 Jun 2007) $
Colin Gillespie (c.gillespie@ncl.ac.uk)
'''
try:
	from ZSI.client import Binding
	from ZSI import ServiceProxy
except ImportError:
	print '##################################################'
	print 'Warning: ZSI is not installed.'
	print '##################################################'
import sys

import BasisWebServices
from GlobalVariables.GlobalAttributes import GlobalParameters

class ConnectToBasis(GlobalParameters):
	
	def __init__(self, **kwargs):
		
		GlobalParameters.__init__(self, **kwargs)
		
		self.s = Binding(host="www.basis.ncl.ac.uk", \
					url='/web-services/dbServer.py',\
					tracefile=sys.stdout, ssl=1)
		
		if not self.username and not self.password:
			print 'Please set you username and password'
			self.s_id = None
		else:
			try:
				self.s_id = str(self.s.getSessionId(self.username, self.password))
			except:
				print 'Your username is %s and password is %s. \
						Is this correct?' % (self.username, self.password)
				self.s_id = 'Not set yet'
		
	def logOut(self):
		'''Logs out from BASIS'''
		self.s.delSessionId(self.s_id)
		return True
	
	def isLoggedIn(self):
		'''Returns True if logged onto BASIS'''
		if self.s.checkSessionId(self.s_id):
			return True
		else:
			return False
	
	def userDetails(self):
		return BasisWebServices.UserDetails(base_values = self, s= self.s)
	
	def model(self):
		return BasisWebServices.Model(base_values = self, s= self.s)
	
	def modelTable(self):
		return BasisWebServices.ModelTable(base_values = self, s= self.s)
	
	def simulate(self):
		return BasisWebServices.Simulate(base_values = self, s= self.s)
	
if __name__=='__main__':
	
	sbml = open('../test/LV2.xml', 'r').read()
	
	c = ConnectToBasis(username='colin', password='colin',
		model_urn='urn:basis.ncl:model:1246', \
		simulation_group_urn='urn:basis.ncl:model:1246:simulation:10-10:1419')
	#c.simulateModel().simulate('runName', 10, 10, 10)
	mf =  c.modelFile()
	mf.getModel('urn:basis.ncl:model:1246')
	print mf.model_urn
	print c.modelFile().model_urn
	#print c.simulation_group_urn
	#print c.results().getListOfSpecies()
	#print c.results().getData(0, 'X')
	
